package main;

import java.util.HashSet;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;

public class QUHC extends JavaPlugin {
	private HashSet<String> lobby;
	private EventManager eventManager;

	@Override
	public void onEnable() {
		lobby = new HashSet<String>();
		ShapedRecipe glisteningMelon = new ShapedRecipe(new ItemStack(Material.SPECKLED_MELON));
		glisteningMelon.shape("G","M").setIngredient('G', Material.GOLD_BLOCK);
		glisteningMelon.setIngredient('M', Material.MELON);
		Bukkit.addRecipe(glisteningMelon);		
		eventManager = new EventManager(this);
	}

	@Override
	public void onDisable() {
		eventManager.quit();
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		switch (command.getName().toLowerCase()) {
		case "reset":
				boolean allowed = true;
				if (sender instanceof Player) {
					Player player = (Player) sender;
					allowed = player.isOp();
				}
				if (allowed) {
					try {
						Player target = null;
						if (args.length == 1) {
							target = Bukkit.getPlayer(args[0]);
						}
						else {
							if (sender instanceof Player) {
								target = (Player) sender;
							}
						}
						if (target.hasMetadata("QUHC")) {
							target.setHealth(20);
							target.setExp(0);
							target.setFoodLevel(20);
							target.setSaturation(20);
							target.getInventory().clear();
							target.getInventory().setArmorContents(null);	
							for (PotionEffect effect : target.getActivePotionEffects()) {
								target.removePotionEffect(effect.getType());
							}
						}
					}
					catch (Exception e) {
					}
				}
			return true;
		case "lobby":
			if (sender instanceof Player) {
				((Player) sender).sendMessage("Players in lobby: " + lobby.size());
			}
			else {
				Bukkit.getLogger().info("Players in lobby: " + lobby.size());
			}
		default:
			break;
		}
		if (sender instanceof Player) {
			Player player = (Player) sender;
			boolean isPlaying = player.hasMetadata("QUHC");
			switch (command.getName().toLowerCase()) {
			case "join":
				if (isPlaying == false) {
					player.sendMessage("Joining a lobby for the next UHC round");
					lobby.add(player.getName());
					if (lobby.size() == 6) {
						start();
					}
				}
				else {
					player.sendMessage("You are already in a game of UHC!");
				}
				return true;
			case "start":
				if (player.isOp()) {
					if (lobby.size() >= 1) {
						player.sendMessage("Started UHC for those in lobby");
						start();
					}
					else {
						player.sendMessage("No one in lobby!");
					}
				}
				return true;
			}
		}
		return true;
	}

	private void start() {
		eventManager.start(lobby);
		lobby.removeAll(lobby);
	}
}
